﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class ChietTinhLandTour : AdminPageBase
    {

        protected List<CalculatorInfo> LandTourList = new List<CalculatorInfo>();
        protected decimal TotalLandTourPrices = 0;
        protected decimal TotalLandTourPricesAdults = 0;
        protected decimal TotalLandTourPricesChild = 0;
        protected decimal TotalLandTourPricesBaby = 0;

        private List<CalculatorInfo> _calculatorList = new List<CalculatorInfo>();
        private readonly CalculatorController _calculatorController = new CalculatorController();
        protected TourInfo tourInfo = new TourInfo();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                _calculatorList =
                    _calculatorController.ListCalculator()
                        .Where(p => p.TourId == int.Parse(Request.QueryString["TourId"])).ToList();
                tourInfo = new TourController().GetTour(int.Parse(Request.QueryString["TourId"]));
            }
            LoadPartner();
            if (_calculatorList.Count == 0) return;
            RenderForm();
        }

        public void RenderForm()
        {

            if (!String.IsNullOrEmpty(Request.QueryString["TourId"]))
            {
                LandTourList = _calculatorList.Where(p => p.ItemType == 12).ToList();
                var landTourPricesAdults = LandTourList.Sum(p => p.TotalPriceLandTourChilds);
                var landTourPricesChild = LandTourList.Sum(p => p.TotalPriceLandTourAdults);
                var landTourPricesBaby = LandTourList.Sum(p => p.TotalPriceLandTourBabys);


                if (tourInfo.QuantityAdult > 0)
                    TotalLandTourPricesAdults = landTourPricesAdults / tourInfo.QuantityAdult;
                if (tourInfo.QuantityChild > 0)
                    TotalLandTourPricesChild = landTourPricesChild / tourInfo.QuantityChild;
                if (tourInfo.QuantityBaby > 0)
                    TotalLandTourPricesBaby = landTourPricesBaby / tourInfo.QuantityBaby;

                TotalLandTourPrices = landTourPricesAdults +
                                       landTourPricesChild +
                                       landTourPricesBaby;
            }
        }

        public void LoadPartner()
        {
            ddlPartnerLandTour.DataSource = new PartnerController().ListPartner().OrderBy(p => p.PartnerName);
            ddlPartnerLandTour.DataValueField = "PartnerId";
            ddlPartnerLandTour.DataTextField = "PartnerName";
            ddlPartnerLandTour.DataBind();

            ddlPartnerLandTour.Items.Insert(0, new ListItem("--Chọn nhà cung cấp--", "-1"));

        }
    }
}